﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Resource
{
    public partial class Favorite : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }
        /// <summary>
        /// 绑定数据到GridViewResourceList
        /// </summary>
        protected void BindData()
        {
            try
            {
                ExtendBLL.FavoriteResource fr = new ExtendBLL.FavoriteResource();
                DataSet ds;

                int userID = int.Parse(UserID);
                int userType = Base.PrivilegeManager.GetUserTypeCode();
                ds = fr.GetFavorites(userID, userType);

                //if (SearchText.Text.Trim() == "")
                //{
                //    ds = resource.GetList(" 1=1 order by [id] desc");
                //}
                //else
                //{
                //    if (SearchCondition.SelectedValue == "1") { ds = resource.Search(SearchText.Text.Trim(), ExtendBLL.Resource.SearchCondition.ByTitle); } // 按标题
                //    else { ds = resource.Search(SearchText.Text.Trim(), ExtendBLL.Resource.SearchCondition.ByTag); }
                //}

                ds = HandleData(ds);

                GridViewResourceList.DataSource = ds.Tables[0].DefaultView;
                GridViewResourceList.DataBind();
            }
            catch { }
        }

        /// <summary>
        /// 处理DS
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        protected DataSet HandleData(DataSet ds)
        {
            ds.Tables[0].Columns.Add("LabName");
            ds.Tables[0].Columns.Add("RecorderName");
            ds.Tables[0].Columns.Add("AttatchmentPath");

            LabMS.Model.Lab labInfo;
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();

            for (int i = ds.Tables[0].Rows.Count - 1; i > -1; i--)
            {
                int labID = int.Parse(ds.Tables[0].Rows[i]["LabID"].ToString());
                labInfo = lab.GetModel(labID);

                ds.Tables[0].Rows[i]["LabName"] = (labInfo != null) ? labInfo.Lab_Name : "全部";

                if (ds.Tables[0].Rows[i]["ResourceRecorderType"].ToString() == "0") // 管理员
                {
                    BLL.UserTable ut = new LabMS.BLL.UserTable();
                    LabMS.Model.UserTable utInfo = ut.GetModel(int.Parse(ds.Tables[0].Rows[i]["ResourceRecorder"].ToString()));

                    ds.Tables[0].Rows[i]["RecorderName"] = (utInfo == null) ? "<del>已删除管理员</del>" : utInfo.UserName + " 管理员";
                }
                else
                {
                    BLL.Teacher teacher = new LabMS.BLL.Teacher();
                    LabMS.Model.Teacher teacherInfo = teacher.GetModel(int.Parse(ds.Tables[0].Rows[i]["ResourceRecorder"].ToString()));

                    ds.Tables[0].Rows[i]["RecorderName"] = (teacherInfo == null) ? "<del>已删除老师</del>" : teacherInfo.Teacher_Name + " 老师";
                }

                ds.Tables[0].Rows[i]["AttatchmentPath"] = ds.Tables[0].Rows[i]["ResourceAttachmentPath"];
            }

            return ds;
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                CheckBox cb;
                int resourceID;
                ExtendBLL.FavoriteResource fr = new ExtendBLL.FavoriteResource();
                LabMS.Model.FavoriteResource frInfo = new LabMS.Model.FavoriteResource();

                for (int i = GridViewResourceList.Rows.Count - 1; i > -1; i--)
                {
                    cb = (CheckBox)GridViewResourceList.Rows[i].FindControl("CBSelect");

                    if (cb.Checked)
                    {
                        resourceID = int.Parse(GridViewResourceList.DataKeys[i].Value.ToString());

                        frInfo.ResourceID = resourceID;
                        frInfo.UserID = int.Parse(UserID);
                        frInfo.UserType = Base.PrivilegeManager.GetUserTypeCode();

                        fr.Delete(frInfo);
                    }
                }

                BindData();

                WriteBackScript("alert('已成功删除相关资源')");
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 写回脚本
        /// </summary>
        /// <param name="script">脚本内容</param>
        protected void WriteBackScript(string script)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                Guid.NewGuid().ToString(), "<script type='text/javascript'>" + script + "</script>");
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewResourceList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewResourceList.PageIndex = e.NewPageIndex;
            BindData();
        }

        /// <summary>
        /// 搜索
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            GridViewResourceList.PageIndex = 0;
            BindData();
        }
    }
}
